#include "graphicbutton.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGridLayout>
#include <QPalette>
#include "buzzer.h"

GraphicButton::GraphicButton(int gra_item_index,QWidget *parent) :
    QPushButton(parent),graphic_btn_index(gra_item_index),flag(true)
{
    setStyleSheet("GraphicButton {background-color:green}");
    this->setText(QString("未定义"));

    device_logo = new QLabel(this);
    geo_descrption = new QLabel("", this);
    geo_descrption->setAlignment(Qt::AlignCenter);
    start_led = new QLabel(this);
    fedback_led = new QLabel(this);
    fault_led = new QLabel(this);
    label_number = new QLabel("", this);
    number =new QLabel("", this);
    number->setAlignment(Qt::AlignCenter);

    QFont ft = label_number->font();
    ft.setPointSize(8);
    label_number->setFont(ft);
    number->setFont(ft);
    geo_descrption->setFont(ft);
    start_led->setFont(ft);
    fedback_led->setFont(ft);
    fault_led->setFont(ft);

    blink_timer = new QTimer(this);
    connect( this, SIGNAL(clicked()), this, SLOT(gra_btn_clicked()) );
    connect( this, SIGNAL(sig_gra_btn_clicked(int)), this->parentWidget(), SLOT(gra_btn_clicked_msg(int)) );
    connect( blink_timer, SIGNAL(timeout()), this, SLOT(blink_led()) );

    //设置layout策略
    QSizePolicy policy = sizePolicy();
    policy.setHorizontalPolicy(QSizePolicy::Expanding);
    policy.setVerticalPolicy(QSizePolicy::Expanding);
    setSizePolicy(policy);

    policy = geo_descrption->sizePolicy();
    policy.setHorizontalPolicy(QSizePolicy::Expanding);
    policy.setVerticalPolicy(QSizePolicy::Expanding);
    geo_descrption->setSizePolicy(policy);

    policy = label_number->sizePolicy();
    policy.setHorizontalPolicy(QSizePolicy::Expanding);
    policy.setVerticalPolicy(QSizePolicy::Expanding);
    label_number->setSizePolicy(policy);

    policy = number->sizePolicy();
    policy.setHorizontalPolicy(QSizePolicy::Expanding);
    policy.setVerticalPolicy(QSizePolicy::Expanding);
    number->setSizePolicy(policy);

    policy = start_led->sizePolicy();
    policy.setHorizontalPolicy(QSizePolicy::Expanding);
    policy.setVerticalPolicy(QSizePolicy::Expanding);
    start_led->setSizePolicy(policy);

    policy = fedback_led->sizePolicy();
    policy.setHorizontalPolicy(QSizePolicy::Expanding);
    policy.setVerticalPolicy(QSizePolicy::Expanding);
    fedback_led->setSizePolicy(policy);

    policy = fault_led->sizePolicy();
    policy.setHorizontalPolicy(QSizePolicy::Expanding);
    policy.setVerticalPolicy(QSizePolicy::Expanding);
    fault_led->setSizePolicy(policy);

    policy = device_logo->sizePolicy();
    policy.setHorizontalPolicy(QSizePolicy::Expanding);
    policy.setVerticalPolicy(QSizePolicy::Expanding);
    device_logo->setSizePolicy(policy);

    //布局管理1
//    QHBoxLayout *toplayout = new QHBoxLayout;
//    toplayout->addWidget(geo_descrption);

//    QVBoxLayout *rightlayout = new QVBoxLayout;
////    rightlayout->addWidget(label_number, 0, 0);
//    rightlayout->addWidget(number);
//    rightlayout->addWidget(start_led);
//    rightlayout->addWidget(fedback_led);
//    rightlayout->addWidget(fault_led);

//    QHBoxLayout *bottomlayout = new QHBoxLayout;
//    bottomlayout->addWidget(device_logo);
//    bottomlayout->addLayout(rightlayout);

//    QVBoxLayout *mainlayout = new QVBoxLayout;
//    mainlayout->addLayout(toplayout);
//    mainlayout->addLayout(bottomlayout);
//    setLayout(mainlayout);

    //布局管理2
    QHBoxLayout *toplayout = new QHBoxLayout;
    toplayout->addWidget(geo_descrption);

    QHBoxLayout *rlayout1 = new QHBoxLayout;
    rlayout1->addWidget(label_number,1);
    rlayout1->addWidget(number,2);
    QHBoxLayout *rlayout2 = new QHBoxLayout;
    rlayout2->addWidget(start_led);
    rlayout2->addWidget(fedback_led);
    rlayout2->addWidget(fault_led);
    QVBoxLayout *rightlayout = new QVBoxLayout;
    rightlayout->addLayout(rlayout1);
    rightlayout->addLayout(rlayout2);


    QHBoxLayout *bottomlayout = new QHBoxLayout;
    bottomlayout->addWidget(device_logo);
    bottomlayout->addLayout(rightlayout);

    QVBoxLayout *mainlayout = new QVBoxLayout;
    mainlayout->addLayout(toplayout);
    mainlayout->addLayout(bottomlayout);
    setLayout(mainlayout);
}


void GraphicButton::set_undefined_state()
{
    this->setText("未定义");
    set_geo_description_and_num("","");
    set_start_led(UNDEFINED);
    set_fedback_led(UNDEFINED);
    set_fault_led(UNDEFINED);
    start_led->setText("");
    fedback_led->setText("");
    fault_led->setText("");
    label_number->clear();
    number->clear();
    device_logo->clear();
}


void GraphicButton::init_const_member()
{
    this->setText("");
    label_number->setText(QString("编号"));
}

void GraphicButton::set_geo_description_and_num(const QString &geo_inf, const QString &num)
{
    geo_descrption->setText(geo_inf);
    number->setText(num);
}

void GraphicButton::set_start_led(led_mode flag)
{
    start_led->setText(QString("启动"));
    if(flag == TURN_OFF)
    {
        start_led->setStyleSheet("QLabel{background-color:gray;}");
    }
    else if(flag == TURN_ON)
    {
        start_led->setStyleSheet("QLabel{background-color:red;}");
    }
    else if(flag == BLINK)
    {
        blink_timer->start(500);
    }
    else if(flag == BLINK_OFF)
    {
        blink_timer->stop();
    }
    else
    {
        start_led->setStyleSheet("QLabel{background-color:green;}");
    }
}


void GraphicButton::set_fedback_led(led_mode flag)
{
    fedback_led->setText(QString("反馈"));
    if(flag == TURN_OFF)
    {
        fedback_led->setStyleSheet("QLabel{background-color:gray;}");
    }
    else if(flag == TURN_ON)
    {
        fedback_led->setStyleSheet("QLabel{background-color:red;}");
    }
    else if(flag == BLINK)
    {}
    else
    {
        fedback_led->setStyleSheet("QLabel{background-color:green;}");
    }
}

void GraphicButton::set_fault_led(led_mode flag)
{
    fault_led->setText(QString("故障"));
    if(flag == TURN_OFF)
    {
        fault_led->setStyleSheet("QLabel{background-color:gray;}");
    }
    else if(flag == TURN_ON)
    {
        fault_led->setStyleSheet("QLabel{background-color:yellow;}");
    }
    else if(flag == BLINK)
    {}
    else
    {
        fault_led->setStyleSheet("QLabel{background-color:green;}");
    }
}

void GraphicButton::set_device_logo(const QString &filepath)
{
    QPixmap   pixmap(48,48);
    pixmap.load(filepath);
    if( filepath == "" )
        device_logo->clear();
    else
        device_logo->setPixmap(pixmap);
}

void GraphicButton::blink_led()
{
    flag = !flag;
    if(flag)
    {
        set_start_led(TURN_ON);
    }
    else
    {
        set_start_led(TURN_OFF);
    }
}

void GraphicButton::gra_btn_clicked()
{
    Buzzer::Instance()->start_buzzer();
    //获取当前按键的编号值
    int btn_number = number->text().toInt();

//    set_start_led(TURN_ON);
    set_start_led(BLINK);
    emit sig_gra_btn_clicked(btn_number);
}
